/*食谱界面*/
var bulletScreenInterval;
$(function () {
    $('#bsTip').fadeIn('slow');
    // bulletScreenInterval = window.setInterval('bulletScreen()', maxTime);
    bulletScreen();
});


var index = 0;

/**
 * 弹幕
 */
 function bulletScreen() {
     var cookbookid = $('#bulletScreen').attr('data');
    //延迟时间
    var timeOut = 3000;
    $.getJSON('/bulletScreen', {"cookbookid":cookbookid}, function (result) {
        if(result.length == 0) {
            window.clearInterval(bulletScreenInterval);
        } else {
            $('#bsTip').fadeOut('slow');
            for (; index < result.length; index++) {
                window.setTimeout('addBulletScreen(' + index + ',' + '\'' + result[index] + '\')', index / 10 * timeOut);
            }
        }
        timeOut = 0;
    });
};

/**
 * 添加弹幕
 */
function addBulletScreen(index, content) {
    //距离顶部位置
    var topDis = 30 * (index % 10);
    var idIndex = 'bs_' + index;
    var clazz = 'screen-word';

    //红色--x上限，y下限
    var x = 200, y = 100;
    var rValue = parseInt(Math.random() * (x - y + 1) + y);
    //绿色--x上限，y下限
    x = 200;
    y = 100;
    var gValue = parseInt(Math.random() * (x - y + 1) + y);
    //蓝色--x上限，y下限
    x = 200;
    y = 100;
    var bValue = parseInt(Math.random() * (x - y + 1) + y);

    var screenWord =
        '<div id="' + idIndex + '" class="'+clazz+
        '" style="top:' + topDis + 'px;color:rgb('+rValue+', '+gValue+', '+bValue+')">' + content +  '</div>';

    //增加dom节点
    $('#bulletScreen').append(screenWord);
    addBSAnimation(idIndex);
}


/**
 * 添加弹幕动画
 */
function addBSAnimation(idIndex) {
    //动画速度--x上限，y下限
    var x = 30, y = 20;
    var speed = parseInt(Math.random() * (x - y + 1) + y) * 1000;

    //添加动画
    var idSelector = '#'+idIndex;
    var width = $(idSelector).outerWidth(true);
    $(idSelector).css({left:'-'+width+'px'});
    $(idSelector).animate({left:'990px'}, speed, function () {
        this.remove();
        var bs = $('#bulletScreen').children('.screen-word');
        if(bs .length > 10 && bs.length <= 20){
            bulletScreen();
        }
    });
    //暂停动画
    $(idSelector).mouseenter(function () {
        $(idSelector).stop();
    });
    //继续执行动画
    $(idSelector).mouseout(function () {
        $(idSelector).animate({left:(990+width)+'px'}, 3000, function () {
            this.remove();
            var bs = $('#bulletScreen').children('.screen-word');
            if(bs .length > 10 && bs.length <= 20){
                console.info(bs.length);
                bulletScreen();
            }
        });
    });
}


function discussSubmit() {
    if(isEmpty($('#discussContent').val())) {
        toast('内容不能为空哟！');
        return ;
    }
    var data = {
        "cookbookid":$('#discussSubmit').attr('data'),
        "content":$('#discussContent').val()
    };
    $.getJSON('/discuss', data, function (result) {
        if(result.code == -1) {
            toast('请先登录再进行评论！');
            return ;
            //收藏
        } else if(result.code == 1) {
            $('#discussContent').val('');
            toast('成功弹了一下！');
            bulletScreen();
        } else {
            toast('哎呀！神评失败！');
        }
    })
}
